package cn.cqautotest.cqviehelper.adapters;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import cn.cqautotest.cqviehelper.R;
import cn.cqautotest.cqviehelper.beans.WeekNumbers;

import java.util.ArrayList;
import java.util.List;

public class ItemRvClassDetailAdapter extends RecyclerView.Adapter<ItemRvClassDetailAdapter.ViewHolder> {

    private List<WeekNumbers> objects = new ArrayList<>();
    private final List<Boolean> mEnables = new ArrayList<>();

    private final Context context;
    private final LayoutInflater layoutInflater;

    public ItemRvClassDetailAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setObjects(List<WeekNumbers> objects) {
        this.objects = objects;
        for (int i = 0; i < objects.size(); i++) {
            mEnables.add(true);
        }
        notifyDataSetChanged();
    }

    public List<WeekNumbers> getObjects() {
        return objects;
    }

    public void setDisEnables() {
        for (int i = 0; i < mEnables.size(); i++) {
            mEnables.set(i, false);
        }
        notifyDataSetChanged();
    }

    public void setEnables() {
        for (int i = 0; i < mEnables.size(); i++) {
            mEnables.set(i, true);
        }
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return objects.size();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.item_rv_class_detail, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Boolean enable = mEnables.get(position);
        WeekNumbers weekNumber = objects.get(position);
        TextView tvWeekNumber = holder.tvWeekNumber;
        tvWeekNumber.setEnabled(enable);
        int colorLightGray = context.getResources().getColor(R.color.colorLightGray);
        int colorBlue = context.getResources().getColor(R.color.blue);
        GradientDrawable gradientDrawable = (GradientDrawable) tvWeekNumber.getBackground();
        if (weekNumber.isSelected()) {
            gradientDrawable.setColor(colorBlue);
            tvWeekNumber.setTextColor(Color.WHITE);
        } else {
            gradientDrawable.setColor(colorLightGray);
            tvWeekNumber.setTextColor(Color.parseColor("#A8A8A8"));
        }
        tvWeekNumber.setText(String.valueOf(weekNumber.getWeekNumber()));
        tvWeekNumber.setOnClickListener(v -> {
            weekNumber.setSelected(!weekNumber.isSelected());
            notifyItemChanged(position, objects.size());
        });
    }

    private final List<Integer> selectedWeeks = new ArrayList<>();

    /**
     * 获取已经选中的周数
     *
     * @return
     */
    public List<Integer> getSelectedWeeks() {
        for (int i = 0; i < objects.size(); i++) {
            WeekNumbers weekNumber = objects.get(i);
            if (weekNumber.isSelected()) {
                selectedWeeks.add(weekNumber.getWeekNumber());
            }
        }
        return selectedWeeks;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvWeekNumber;

        ViewHolder(View view) {
            super(view);
            tvWeekNumber = view.findViewById(R.id.tvWeekNumber);
        }
    }
}
